// MIT License
//
// Copyright(c) 2021 Jaf932074323
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this softwareand associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright noticeand this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// 2025-5-6 姜安富
#include "kmp.h"
#include "gtest/gtest.h"
#include <algorithm>
#include <array>
#include <format>

namespace
{

template <size_t TextSize, size_t PatternSize>
void TestEquality(const char (&text)[TextSize], const char (&pattern)[PatternSize])
{
    size_t my_first_index = jaf::kmp::Search(text, pattern);
    size_t my_end_index   = jaf::kmp::SearchEnd(text, pattern);

    size_t std_first_index = std::search(text, text + TextSize - 1, pattern, pattern + sizeof(pattern) - 1) - text;
    size_t std_end_index   = std::find_end(text, text + TextSize - 1, pattern, pattern + sizeof(pattern) - 1) - text;

    EXPECT_EQ(my_first_index, std_first_index) << std::format("主串:{}, TextSize:{},子串{},PatternSize:{}", text, TextSize, pattern, PatternSize);
    EXPECT_EQ(my_end_index, std_end_index) << std::format("主串:{}, TextSize:{},子串{},PatternSize:{}", text, TextSize, pattern, PatternSize);
}

} // namespace

TEST(kmp, usually)
{
    TestEquality("abccab", "ab");
    TestEquality("ABABDABACDABABCABAB", "ABABCABAB");
    TestEquality("ABCABCD", "ABCD");
    TestEquality("ABC ABCDAB ABCDABCDABDE", "ABCD");
    TestEquality("ABC ABCDAB ABCDABCDABDE", "XYZ");
    TestEquality("", "ABC");
    TestEquality("你好", "好");
    TestEquality("发明和发现的区别", "发");
    //TestEquality("ABC", ""); // 编译错误

}
